<?php
namespace app\shop\model;

use app\BaseModel;
use think\facade\Db;
//use app\shop\model\AdminShopType;
//use app\shop\model\AdminShopSpePrice;

class AdminGoods extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_goods';


    /**
     * @var array
     * 查询器
     */
    protected $append = [
        'cate_text',
        'cate_reset_text',
        'price_text',
        'company_count_text',
    ];
    /**
     * User: chenniang
     * Date: 2019-09-10 18:29
     * @param $query
     * @param $status
     * @return void
     * descption:查询范围
     */
    public function scopeStatus($query, $status){
        $query->where('status', '>', $status);
    }

    /**
     * User: chenniang
     * Date: 2019-09-24 18:59
     * @param $value
     * @return string
     * descption:转富文本
     */
    public function getContentAttr($value){
        return html_entity_decode($value);
    }
    /**
     * User: chenniang
     * Date: 2019-09-24 10:59
     * @param $value
     * @param $data
     * @return int
     * descption:获取公司个数
     */
    public function getCompanyCountTextAttr($value,$data){

        if(!empty($data['public_goods'])){

            $company_model = new AdminCompany();

            $dis = [

                'uniacid' => $data['uniacid'],

                'status'  => 1
            ];

            $count = $company_model->where($dis)->count();

        }else{

            $company_model = new AdminCompanyGoods();

            $dis = [
                'a.uniacid' => $data['uniacid'],

                'a.goods_id'=> $data['id'],

                'a.status'  => 1,

                'b.status'  => 1
            ];

            $count = $company_model->getCount($dis);
        }

        return $count;
    }
    /**
     * @param $value
     * @param $data
     * @return mixed
     * 分类名字
     */
    public function getCateTextAttr($value,$data){
        $cate_model = new AdminShopType();
        $cate = $cate_model->cateTitle(['id'=>$data['type']]);
        return $cate;
    }
    /**
     * @param $value
     * @param $data
     * @return mixed
     * 分类名字
     */
    public function getCateResetTextAttr($value,$data){
        $cate_model = new AdminShopType();
        $cate = $cate_model->reset($data['type']);
        return $cate;
    }
    /**
     * @param $value
     * @return mixed|string
     * 转换图片
     */
    public function getCoverAttr($value){
        $data = transImages([$value],['cover']);
        return !empty($data[0])?$data[0]:[];
    }
    /**
     * @param $value
     * @return mixed|string
     * 转换视频
     */
    public function getVideoAttr($value){
        $data = transImagesOne([$value],['video']);
        return !empty($data[0])?$data[0]:'';
    }
    /**
     * @param $value
     * @return mixed|string
     * 转换视频
     */
    public function getVideoCoverAttr($value){
        $data = transImages([$value],['video']);
        return !empty($data[0])?$data[0]:'';
    }
    /**
     * @param $value
     * @return mixed|string
     * 转换图片
     */
    public function getImagesAttr($value){
        $data = transImages([$value],['images']);
        return !empty($data[0])?$data[0]:[];
    }
    /**
     * User: chenniang
     * Date: 2019-09-20 19:08
     * @param $value
     * @return int
     * descption:价格转成int
     */
    public function getPriceAttr($value){
        return intval($value);
    }
    /**
     * User: chenniang
     * Date: 2019-09-23 15:24
     * @return void
     * descption:获取规格的最小价格
     */
    public function getPriceTextAttr($value,$data){
       $spe_price = new AdminShopSpePrice();
       return $spe_price->where(['status'=>1,'goods_id'=>$data['id']])->min('price');
    }


    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2020-06-03 13:15
     */
    public function getTemplateIdAttr($value,$data){

        return !empty($value)?$value:'';

    }
    /**
     * User: chenniang
     * Date: 2019-09-23 16:28
     * @param $dis
     * @param int $page
     * @return array
     * descption:商品列表
     */
    public function goodList($dis,$page=10){

        $data = $this->where($dis)->order('recommend desc,top desc,update_time desc')->paginate($page)->toArray();

        return $data;
    }
    /**
     * User: chenniang
     * Date: 2019-09-23 16:28
     * @param $dis
     * @param $data
     * @return AdminGoods
     * descption:修改商品内容
     */
    public function goodsUpdate($dis,$data){

        if(isset($data['is_parameter'])&&$data['is_parameter']==0){

            $data['template_id']=0;
        }

        $data['update_time'] = time();

        $res = self::where($dis)->update($data);

        return $res;
    }
    /**
     * User: chenniang
     * Date: 2019-09-23 16:29
     * @param $data
     * @param $id
     * @return AdminGoods
     * descption:修改商品状态
     */
    public function goodsUpdateStatus($data,$id){
        $data['update_time'] = time();
        $res = self::where('id','in',$id)->update($data);
        return $res;
    }
    /**
     * User: chenniang
     * Date: 2019-09-23 16:29
     * @param $data
     * @return int|mixed|string
     * descption:添加商品
     */
    public function goodsAdd($data){
        if($data['is_parameter']==0){
            $data['template_id']=0;
        }
        $data['create_time'] = time();
        $data['status']      = 1;
        $res = $this->insert($data);
        $res = $this->getLastInsID();
        return $res;
    }
    /**
     * User: chenniang
     * Date: 2019-09-23 16:29
     * @param $dis
     * @return array|\think\Model|null
     * descption:商品详情
     */
    public function goodsInfo($dis){
        $data = self::where($dis)->find();
        $data = !empty($data)?$data->toArray():$data;
        return $data;
    }
    /**
     * User: chenniang
     * Date: 2019-09-23 16:29
     * @param $dis
     * @return array|\think\Collection
     * descption:商品分类
     */
    public function goodsSelect($dis){
        $data = self::where($dis)->select();
        return !empty($data)?$data->toArray():$data;
    }
    /**
     * User: chenniang
     * Date: 2019-09-23 16:29
     * @param $input
     * @return mixed
     * descption:转换商品插入数据
     */
    public function returnData($input){
        $data['name']       = trim($input['basicform']['name']);
        $data['s_title']    = $input['basicform']['s_title'];
        $data['type_p']     = $input['basicform']['type'][0];
        $data['type']       = array_pop($input['basicform']['type']);
        $data['price']      = $input['setForm']['price'];
        $data['top']        = $input['relevantForm']['top'];
//        $data['stock']      = $input['relevantForm']['stock'];
        $data['unit']       = $input['relevantForm']['unit'];
        $data['freight']    = $input['relevantForm']['freight'];
        $data['view_count'] = $input['relevantForm']['view_count'];
        $data['sale_count'] = $input['relevantForm']['sale_count'];
        $data['cover']      = implode(',',$input['imgForm']['cover']);
        $data['images']     = implode(',',$input['imgForm']['images']);
        $data['video']      = $input['imgForm']['video'];
        $data['video_cover']= implode(',',$input['imgForm']['video_cover']);
        $data['image_url']  = $input['imgForm']['image_url'];
        $data['content']    = $input['imgForm']['content'];
        $data['recommend']  = $input['setForm']['recommend'];

        $data['index_show'] = isset($input['setForm']['index_show'])?$input['setForm']['index_show']:1;

        $data['is_self']    = !empty($input['setForm']['is_self'])?$input['setForm']['is_self']:0;

        $data['is_send']    = !empty($input['setForm']['is_send'])?$input['setForm']['is_send']:0;

        $data['switch']     = $input['setForm']['switch'];

        $data['extract']    = $input['setForm']['extract'];

        $data['staff_switch']  = $input['setForm']['staff_switch'];

        $data['staff_extract'] = $input['setForm']['staff_extract'];

        $data['is_member']     = $input['memberForm']['is_member'];

        $data['is_discount']   = $input['memberForm']['is_discount'];

        $data['template_id']   = $input['basicform']['template_id']??0;

        $data['is_parameter']  = $input['basicform']['is_parameter']??0;

        $data['is_limit'] = $input['setForm']['is_limit']??1;

        if(isset($input['setForm']['limit'])){

            $data['limit'] = $input['setForm']['limit'];
        }
        $data['start_min'] = $input['setForm']['start_min']??1;

        $data['public_goods'] = $input['all_company']??0;

        if(isset($input['setForm']['staff_extract_cash'])){

            $data['staff_extract_cash'] = $input['setForm']['staff_extract_cash'];
        }
        if(isset($input['setForm']['extract_cash'])){

            $data['extract_cash'] = $input['setForm']['extract_cash'];
        }
//        $data['is_parameter'] = !empty($input['is_parameter'])?$input['is_parameter']:0;
        return $data;
    }
    /**
     * User: chenniang
     * Date: 2019-09-10 18:25
     * @param $dis
     * @return void
     * descption:获取商品id
     */
    public function goodsId($dis){
        $data = $this->where($dis)->status(-1)->column('id');
        return !empty($data)?array_values($data):$data;
    }


    /**
     * User: chenniang
     * Date: 2019-10-11 15:07
     * @param $dis
     * @return mixed
     * descption:商品数量
     */
    public function goodsCount($dis){
        return $this->where($dis)->count();
        //return $this->where($dis)->status(-1)->count();
    }


    /**
     * @author chenniang
     * @DataTime: 2020-08-25 17:19
     * @功能说明:
     */
    public function saleIngCount($uniacid,$type=1){

        $spe_price_model = new AdminShopSpePrice();

        $dis  = [];

        $dis[]= ['uniacid','=',$uniacid];

        $sale_type      = $type==2?0:1;

        $sale_out_goods = $spe_price_model->getSellOut($uniacid,$sale_type);

        switch ($type){

            case 1:
                $dis[] = ['status','=',1];
                break;

            case 2:

                $dis[] = ['status','>',-1];
                break;

            case 3:
                $dis[] = ['status','=',0];
                break;

        }

        $dis[] = ['id','in',$sale_out_goods];

        $data = $this->goodsCount($dis);

        return $data;
    }

    /**
     * @param $where
     * @功能说明:查询是否有商品使用某个模板
     * @author lxj
     * @DataTime: 2020/5/19 16:05
     */
    public function getHistory($where){

        return $this->where($where)->order('update_time desc')->limit(1)->field('id')->find();
    }

    /**
     * @param $where
     * @功能说明:查询某个模板是否被使用
     * @author lxj
     * @DataTime: 2020/5/21 20:15
     */
    public function isUse($where){
        $res = $this->where($where)->where('status','<>',-1)->find();
        return $res;
    }



}